package com.wss.common.base;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bingoogolapple.refreshlayout.BgarefreshLayout;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.wss.common.bean.Event;
import com.wss.common.utils.EventBusUtils;
import com.wss.common.widget.ActionBar;
import ohos.aafwk.ability.ILifecycleObserver;
import ohos.aafwk.ability.LifecycleObserver;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.PixelMapElement;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;

public abstract class BaseAbility extends FractionAbility {

    private Unbinder unbinder;
    private Component emptyView;
    protected Context context;

    protected ActionBar actionBar;

    protected Component actionBarLine;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        //加入Ability管理器
        BaseApplication.i().getAbilityManage().addAbility(this);
        context = this;

        if (!isFullScreenLayout()) {
            setUIContent(ResourceTable.Layout_ability_base);
            ((ComponentContainer) findComponentById(ResourceTable.Id_fl_content))
                    .addComponent(LayoutScatter.getInstance(this).parse(getLayoutId(), null, false));
            unbinder = ButterKnife.bind(this);
            findView();
        }
        if (registerEventBus()) {
            EventBusUtils.register(this);
        }
    }

    private void findView() {
        actionBar = (ActionBar) findComponentById(ResourceTable.Id_actionbar);
        actionBarLine = findComponentById(ResourceTable.Id_action_bar_line);
    }

    /**
     * 设置沉浸栏颜色
     *
     * @param statusBarColor 颜色
     */
    protected void setImmersionBarColor(int statusBarColor) {
        setImmersionBarColor(statusBarColor, false);
    }

    /**
     * 设置沉浸栏颜色
     *
     * @param statusBarColor 沉浸栏颜色
     */
    protected void setImmersionBarColor(int statusBarColor, boolean fitsSystemWindows) {
        setImmersionBarColor(statusBarColor, ResourceTable.Color_black, fitsSystemWindows);
    }

    /**
     * 设置沉浸栏颜色
     *
     * @param statusBarColor     沉浸栏颜色
     * @param navigationBarColor 沉浸栏图标颜色
     */
    protected void setImmersionBarColor(int statusBarColor, int navigationBarColor, boolean fitsSystemWindows) {
        ImmersionBar.with(this)
                .statusBarColor(statusBarColor)
                .navigationBarColor(navigationBarColor)
                .fitsSystemWindows(fitsSystemWindows)
                .autoDarkModeEnable(true)
                .statusBarDarkFont(true)
                .init();
    }

    /**
     * 隐藏状态栏、导航栏
     */
    protected void setImmersionBarHide() {
        ImmersionBar.with(this)
                .fitsSystemWindows(false)
                .hideBar(BarHide.FLAG_HIDE_BAR)
                .init();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (unbinder != null) {
            unbinder.unbind();
        }
        if (registerEventBus()) {
            EventBusUtils.unregister(this);
        }
        //将Activity从管理器移除
        BaseApplication.i().getAbilityManage().removeAbility(this);
    }

    //***************************************空页面方法 start*************************************

    /**
     * 数据为空页面
     */
    protected void showEmptyView() {
        showEmptyView(getString(ResourceTable.String_no_data));
    }

    /**
     * 数据为空页面
     *
     * @param text 显示文案
     */
    protected void showEmptyView(String text) {
        showEmptyOrErrorView(text, ResourceTable.Media_bg_no_data, false);
    }

    /**
     * 请求数据报错页面
     */
    protected void showErrorView() {
        showErrorView(getString(ResourceTable.String_network_error_server_error));
    }

    /**
     * 请求数据报错页面
     *
     * @param text 显示文案
     */
    protected void showErrorView(String text) {
        showEmptyOrErrorView(text, ResourceTable.Media_bg_no_net, true);
    }

    /**
     * 请求数据报错或者为空页面
     *
     * @param text      提示文案
     * @param iconResId 显示的icon
     */
    public void showEmptyOrErrorView(String text, int iconResId, boolean showRefreshButton) {
        if (emptyView == null) {
            emptyView = findComponentById(ResourceTable.Id_vs_empty);
        }
        emptyView.setVisibility(Component.VISIBLE);
        ((Image) findComponentById(ResourceTable.Id_iv_empty))
                .setPixelMap(iconResId);
        ((Text) findComponentById(ResourceTable.Id_tv_empty)).setText(text);
        if (showRefreshButton) {
            Component refreshButton = findComponentById(ResourceTable.Id_tv_try_again);
            refreshButton.setVisibility(Component.VISIBLE);
            refreshButton.setClickedListener(v -> onRefreshRetry());
        }
    }

    /**
     * 隐藏空页面
     */
    protected void hideEmptyView() {
        if (emptyView != null) {
            emptyView.setVisibility(Component.HIDE);
        }
    }

    /**
     * 刷新重试
     */
    protected void onRefreshRetry() {
    }

    //***************************************空页面方法 end*********************************

    /**
     * 停止刷新和加载更多
     *
     * @param layout 刷新layout
     */
    protected void stopRefresh(BgarefreshLayout layout) {
        if (layout == null) {
            return;
        }
        layout.endLoadingMore();
        layout.endRefreshing();
    }

    /**
     * 是否布局伸入状态栏类型Activity
     *
     * @return boolean
     */
    protected boolean isFullScreenLayout() {
        return false;
    }

    /**
     * 需要接收事件 重写该方法 并返回true
     *
     * @return boolean
     */
    protected boolean registerEventBus() {
        return false;
    }

    /**
     * 子类接受事件 重写该方法
     *
     * @param event 事件
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBus(Event event) {
    }

    /**
     * 返回页面layout
     *
     * @return layout
     */
    protected abstract int getLayoutId();

}
